package com.example.qianjun.ninetable;

/**
 * Created by qianjun on 2016/5/24.
 */

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


/**
 * Created by qianjun on 2016/5/2.
 */
public class NineTable extends View {




    private Paint mPaint = new Paint();

    int  table_x =4;

    private int mPanelwidth;
    private float mLineheight;


    //对话框
    private AlertDialog.Builder builder;


    //初始化
    public void init() {

        //画笔设置
        mPaint.setAlpha(10);
        mPaint.setColor(Color.rgb(0x8b, 0x8b, 0x83));
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setTextSize(GlobalUtil.TEXT_SIZE);
        mPaint.setTextAlign(Paint.Align.LEFT);

    }

    //构造函数
    public NineTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = Math.min(widthSize, heightSize);
        if (widthMode == MeasureSpec.UNSPECIFIED) {
            width = heightSize;
        } else if (heightMode == MeasureSpec.UNSPECIFIED) {
            width = widthSize;
        }

        setMeasuredDimension(width, width);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mPanelwidth = w;

        mLineheight = mPanelwidth * 1.0f / table_x;


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
        drawPieces(canvas);


    }

    //绘制盘面
    private void drawBoard(Canvas canvas) {
        int w = mPanelwidth;
        float lineHeight = mLineheight;

        mPaint.setColor(Color.rgb(0x8b, 0x8b, 0x83));
        mPaint.setStyle(Paint.Style.STROKE);

        for (int i = 0; i < table_x; i++) {
            int startX = (int) (lineHeight / 2);
            int endX = (int) (w - lineHeight / 2);


            int y = (int) ((0.5 + i) * lineHeight);
            canvas.drawLine(startX, y, endX, y, mPaint);
            canvas.drawLine(y, startX, y, endX, mPaint);

        }

        //mPaint.setColor(Color.RED);

        mPaint.setAlpha(255);
        mPaint.setColor(Color.rgb(0xC1,0xFF,0xC1));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle((float) 1.5*lineHeight,(float) 1.5*lineHeight,mLineheight/4,mPaint);
        canvas.drawCircle((float) 2.5*lineHeight,(float) 1.5*lineHeight,mLineheight/4,mPaint);
        canvas.drawCircle((float) 1.5*lineHeight,(float) 2.5*lineHeight,mLineheight/4,mPaint);
        canvas.drawCircle((float) 2.5*lineHeight,(float) 2.5*lineHeight,mLineheight/4,mPaint);

        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(GlobalUtil.TEXT_SIZE/2);
        canvas.drawText("Ⅰ",(float) 1.5*lineHeight-9,(float) 1.5*lineHeight+4,mPaint);
        canvas.drawText("Ⅱ",(float) 2.5*lineHeight-9,(float) 1.5*lineHeight+4,mPaint);
        canvas.drawText("Ⅲ",(float) 1.5*lineHeight-9,(float) 2.5*lineHeight+4,mPaint);
        canvas.drawText("Ⅳ",(float) 2.5*lineHeight-9,(float) 2.5*lineHeight+4,mPaint);


    }

    //绘制数字
    private void drawPieces(Canvas canvas) {

        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(GlobalUtil.TEXT_SIZE);
        float lineHeight = mLineheight;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int x = (int) ((0.9 + i) * lineHeight);
                int y = (int) ((1.08 + j) * lineHeight);

                canvas.drawText(CheckBoard.table[i][j]+"",x,y,mPaint);
//                if (CheckBoard.table[i][j] != '#') {
//                    canvas.drawText(CheckBoard.table[i][j] + "", x, y, mPaint);
//                }


            }
        }
    }


}